<?php

App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @author OanhNN
 * @property User $User
 * @property UserProfile $UserProfile
 * @property CaptchaComponent $Captcha
 */
class UsersController extends AppController {

    /**
     * uses model
     * @var array
     */
    public $uses = array('User', 'UserProfile');
    /**
     * component
     * @var array
     */
    public $components = array('Captcha');

    /**
     * before process
     * @return void
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array('login', 'forgot_password', 'new_password', 'signup', 'activate', 'captchaImage', 'captchaAudio'));
    }

    /**
     * Login action
     * @author OanhNN
     * @return void
     */
    public function login() {
        if ($this->Auth->loggedIn()) {
            $this->flash('Hiện tại bạn đã đăng nhập. Hệ thống sẽ trở về trang chủ sau 5 giây.', '/', 5);
            return;
        }
        if ($this->request->is('post')) {
            $this->User->set($this->data);
            unset($this->User->validate['email']['isUnique']);
            if ($this->User->validates(array('fieldList' => array('email', 'password')))) {
                if ($this->Auth->login()) {
                    //TODO : Kiểm tra trạng thái của user.
                    $this->User->id = $this->Auth->user('id');
                    $newToken = $this->_randomToken();
                    $this->User->saveField('token', $newToken);
                    $this->User->saveField('last_login', date('Y-m-d H:i:s'));
                    if ($this->data['User']['remember_me'] == '1') {
                        $cookie = array(
                            'email' => $this->Auth->user('email'),
                            'identifier' => $this->Auth->user('identifier'),
                            'token' => $newToken
                        );
                        $this->Cookie->write('User', serialize($cookie), true);
                    }
                    $this->redirect($this->Auth->redirect());
                } else {
                    $this->Session->setFlash('Email hoặc Password không đúng.', FMSG_ERROR);
                    return;
                }
            } else {
                $this->Session->setFlash('Email hoặc Password không hợp lệ.', FMSG_ERROR);
                return;
            }
        }
    }

    /**
     * Logout action
     * @author OanhNN
     * @return void
     */
    public function logout() {
        $this->Cookie->delete('User');
        $this->redirect($this->Auth->logout());
    }

    /**
     * Forgot password action
     * @author OanhNN
     * @return void
     */
    public function forgot_password() {
        
    }

    /**
     * Forgot password action
     * @author OanhNN
     * @return void
     */
    public function new_password() {
        
    }

    /**
     * Forgot password action
     * @author OanhNN
     * @return void
     */
    public function signup() {
        if ($this->Auth->loggedIn()) {
            $this->flash('Hiện tại bạn đã đăng nhập. Hệ thống sẽ trở về trang chủ sau 5 giây.', '/', 5);
            return;
        }
        if ($this->request->is('post')) {
            $this->User->set($this->data);
            if ($this->User->validates(array('fieldList' => array('email', 'password', 'repassword', 'captcha')))) {
                
            } else {
                $this->Session->setFlash('Bạn cần nhập đúng các input.', FMSG_ERROR);
                return;
            }
        }
    }

    /**
     * Forgot password action
     * @author OanhNN
     * @return void
     */
    public function activate() {
        
    }

    /**
     * Create Image Captcha
     * @author OanhNN
     * @return void
     */
    public function captchaImage() {
        $this->Captcha->image();
        $this->header('200');
        $this->response->type('jpg');
        $this->render(null, null);
    }

    /**
     * Create Audio Captcha
     * @author OanhNN
     * @return void
     */
    public function captchaAudio() {
        $this->Captcha->audio();
        $this->header('200');
        $this->response->type('wav');
        $this->render(null, null);
    }

    
    
    
    /**
     * Check vaildate toke submit by URL
     * @author OanhNN
     * @return boolean|array false if invaild
     */
    private function _checkToken() {
        if (!isset($this->request->query['id'])) {
            return false;
        }
        $user = $this->User->find('first', array(
            'conditions' => array('token' => $this->request->query['id'], 'del_flg' => 0),
            'recursive' => -1
                ));
        if ($user) {
            return $user;
        } else {
            return false;
        }
    }

    /**
     * Make random token
     * @author OanhNN
     * @return String
     */
    private function _randomToken() {
        return String::uuid();
    }

    /**
     * Make random password
     * @author OanhNn
     * @return String
     */
    private function _randomPassword() {
        //TODO : viết hàm random password
        return '123456';
    }

}
